package com.ctc.test.serial;

import android.util.Log;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;

/**
 * 串口查找工具
 */
public class SerialPortFinder
{

    private static final String TAG = "SerialPortFinder";
    private static final String DRIVERS_PATH = "/proc/tty/drivers";
    private static final String SERIAL_FIELD = "serial";

    /**
     * 获取串口集合
     */
    public static ArrayList<SerialPortBean> getSerialPort()
    {
        ArrayList<SerialPortBean> list_serialPortBean = new ArrayList<>();
        try
        {
            File file_driver = new File(DRIVERS_PATH);
            if (!file_driver.exists())
            {
                Log.w(TAG, String.format("getSerialPort() ---> %s 不存在", DRIVERS_PATH));
                return list_serialPortBean;
            }
            Log.w(TAG, String.format("getSerialPort() ---> Driver can Read: %s", file_driver.canRead()));

            File dev = new File("/dev");
            if (!dev.exists())
            {
                Log.w(TAG, "getSerialPort() ---> /dev 不存在");
                return list_serialPortBean;
            }
            if (!dev.canRead())
            {
                Log.w(TAG, "getSerialPort() ---> /dev 没有读取权限");
                return list_serialPortBean;
            }

            File[] devices = dev.listFiles();
//            for (File file : devices) {
//                Log.d(TAG, String.format("getSerialPort() ---> Serial path: %s", file.getAbsolutePath()));
//            }

            LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(DRIVERS_PATH));
            String readLine;
            while ((readLine = lineNumberReader.readLine()) != null)
            {
                String driverName = readLine.substring(0, 0x15).trim();
                String[] fields = readLine.split(" +");
                if ((fields.length >= 5) && (fields[fields.length - 1].equals(SERIAL_FIELD)))
                {
                    String devicePath = fields[fields.length - 4];

                    Log.d(TAG, String.format("getSerialPort() ---> Found Device name: %s   path: %s", driverName, devicePath));

                    for (File file : devices)
                    {
                        if (file.getAbsolutePath().startsWith(devicePath))
                        {
                            Log.i(TAG, String.format("getSerialPort() ---> Device path: %s   SerialPort path: %s", devicePath, file.getAbsolutePath()));
                            list_serialPortBean.add(new SerialPortBean(file.getName(), driverName, file));
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Log.w(TAG, String.format("getSerialPort() ---> Error: %s", e.toString()));
            e.printStackTrace();
        }

        return list_serialPortBean;
    }

    /**
     * 获取串口路径集合
     */
    public static ArrayList<String> getSerialPortPath()
    {
        ArrayList<String> list_serialPortName = new ArrayList<>();
        ArrayList<SerialPortBean> list_serialPortBean = getSerialPort();
        for (SerialPortBean serialPortBean : list_serialPortBean)
        {
            list_serialPortName.add(serialPortBean.getPath());
        }
        return list_serialPortName;
    }

}
